/* ---------------------------------------------------------------------------
 * This software is in the public domain, furnished "as is", without technical
 * support, and with no warranty, express or implied, as to its usefulness for
 * any purpose.
 *
 * File.cpp
 *
 * Author: Guillaume Boissé
 * -------------------------------------------------------------------------*/

#include "File.h"
#include "Debug.h"

#include <cstring>

namespace jsp
{

File::File(const s8* pFilename)
{
    m_pFile = fopen(pFilename, "r");
    m_pContent = NULL;

    if (m_pFile)
    {
        s32 tRes = fseek(m_pFile, 0, SEEK_END);
        ASSERT(tRes == 0);

        size_t tSize = ftell(m_pFile) + 1;
        m_pContent = new s8[tSize];
        memset(m_pContent, 0, tSize);

        tRes = fseek(m_pFile, 0, SEEK_SET);
        ASSERT(tRes == 0);

        m_size = fread(m_pContent, 1, tSize, m_pFile);
        ASSERT(m_size == tSize - 1);

        tRes = fclose(m_pFile);
        ASSERT(tRes == 0);
        m_pFile = NULL;
    }
}

File::~File()
{
    if (m_pContent)
    {
        delete[] m_pContent;
        m_pContent = NULL;
    }
}

const s8* File::text() const
{
    return m_pContent;
}

} // namespace jsp
